package edu.bu.grocerylist.service;

import java.util.List;

import edu.bu.grocerylist.dao.IGroceryListDao;
import edu.bu.grocerylist.dao.IGroceryListItemDao;
import edu.bu.grocerylist.model.Category;
import edu.bu.grocerylist.model.GroceryList;
import edu.bu.grocerylist.model.GroceryListItem;
import edu.bu.grocerylist.model.GroceryUser;
import edu.bu.grocerylist.model.Item;

/**
 * This class contains the business logic for when working with GroceryList
 * objects
 * 
 * @author donlanp
 * 
 */
public class GroceryListService implements IGroceryListService {
	
	private IGroceryListDao groceryListDao;
	
	private IGroceryListItemDao groceryListItemDao;

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.service.IGroceryListService#createGroceryList()
	 */
	public GroceryList createGroceryList() {
		return new GroceryList();
	}

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.service.IGroceryListService#templateGroceryList(edu.bu.grocerylist.model.GroceryList)
	 */
	public GroceryList templateGroceryList(GroceryList groceryList) {
		GroceryList newList = new GroceryList(groceryList.getName(),
				groceryList.getDescription(), groceryList.getShoppingDate());
		
		// also need to handle grocery list item.
		for(GroceryListItem g: groceryList.getShoppingItems()){
			GroceryListItem newItem = new GroceryListItem();
			newItem.setStatus(GroceryListItem.NOT_SHOPPED);
			newItem.setCategory(g.getCategory());
			newItem.setName(g.getName());
			newItem.setMeasurement(g.getMeasurement());
			newItem.setQuantity(g.getQuantity());
			newList.getShoppingItems().add(newItem);
		}
		

		return newList;
	}


	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.service.IGroceryListService#getUsersGroceryList(GroceryUser)
	 */
	public List<GroceryList> getUsersGroceryList(GroceryUser user) {
		List<GroceryList> userLists = this.groceryListDao.getGroceryListsByUser(user.getUserId());
		
		for(GroceryList g: userLists){
			List<GroceryListItem> items = groceryListItemDao.getGroceryListItemsByListId(g.getGroceryListId());
			g.setShoppingItems(items);
		}
		
		return userLists;
	}

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.service.IGroceryListService#getGroceryListById(int)
	 */
	public GroceryList getGroceryListById(int groceryListId) {
		return this.groceryListDao.getGroceryListById(groceryListId);
	}

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.service.IGroceryListService#getGroceryListById(int)
	 */
	public GroceryList saveGroceryList(GroceryList list) {
		System.err.println("Save Grocery List...");
		GroceryList saved = this.groceryListDao.saveGroceryList(list);
		//Delete any existing rows in the database (so we can have a fresh start with GroceryList items)
		groceryListItemDao.deleteAllGroceryListItemsFromList(list);
		
		//First save the grocerylist items
		for(GroceryListItem g: list.getShoppingItems()){
			//Check for item name
			Item item = new Item(); 
			if(!groceryListItemDao.doesItemExist(g.getName())){
				item.setItemName(g.getName());
				item = groceryListItemDao.saveNewItem(item);
			} else {
				item = groceryListItemDao.getItemIdByName(g.getName());
			}
			g.setItemId(item.getItemId());
			
			//Check for category
			Category category = new Category();
			if(!groceryListItemDao.doesCategoryExist(g.getCategory())){
				category.setName(g.getCategory());
				category.setDescription(g.getCategoryDescription());
				category = groceryListItemDao.saveNewCategory(category);
			} else {
				category = groceryListItemDao.getCategoryIdByName(g.getCategory());
			}
			g.setCategoryId(category.getCategoryId());
			
			//Save the list item
			if(g.getItemId() == 0){
				g.setStatus(GroceryListItem.NOT_SHOPPED);				
			}
			g.setListId(saved.getGroceryListId());
			groceryListItemDao.insertGroceryListItem(g);
		}
		return saved;
	}

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.service.IGroceryListService#getGroceryListById(int)
	 */
	public boolean deleteGroceryList(GroceryList list) {
		for(GroceryListItem g: list.getShoppingItems()){
			groceryListItemDao.deleteGroceryListItem(g);
		}
		return this.groceryListDao.deleteGroceryList(list);
	}
	
	//Getters and Setters

	/**
	 * @return the groceryListItemDao
	 */
	public IGroceryListItemDao getGroceryListItemDao() {
		return groceryListItemDao;
	}

	/**
	 * @param groceryListItemDao the groceryListItemDao to set
	 */
	public void setGroceryListItemDao(IGroceryListItemDao groceryListItemDao) {
		this.groceryListItemDao = groceryListItemDao;
	}

	public IGroceryListDao getGroceryListDao() {
		return groceryListDao;
	}

	public void setGroceryListDao(IGroceryListDao groceryListDao) {
		this.groceryListDao = groceryListDao;
	}
	
}
